<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="灯控器地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入灯控器地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="端口号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ep">
              <a-input-number v-model="model.ep" placeholder="请输入端口号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="协议" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pid">
              <a-input-number v-model="model.pid" placeholder="请输入协议" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="设备类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="did">
              <j-dict-select-tag type="list" v-model="model.did" dictCode="device_type" placeholder="请选择设备类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="当前亮度状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bri">
              <a-input-number v-model="model.bri" placeholder="当前亮度状态: 1-254" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="色温" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="colorTemp">
              <a-input-number v-model="model.colorTemp" placeholder="色温: 0-100" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="实时电压" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="volt">
              <a-input-number v-model="model.volt" placeholder="实时电压" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="实时电流" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="curr">
              <a-input-number v-model="model.curr" placeholder="请输入实时电流" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="有功功率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="actp">
              <a-input-number v-model="model.actp" placeholder="有功功率" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="有功电能" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="acte">
              <a-input-number v-model="model.acte" placeholder="有功电能" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="运行时间（分）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lightrt">
              <a-input v-model="model.lightrt" placeholder="运行时间（分）" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="经度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gwlongitude">
              <a-input v-model="model.gwlongitude" placeholder="请输入经度"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="纬度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gwlatitude">
               <a-row :gutter="24">
                <a-col :span="16">
                <a-input v-model="model.gwlatitude" placeholder="请输入纬度"  ></a-input>
                </a-col>
                <a-col :span="8">
                <a-button @click="openSetLocation()" type="primary">选择</a-button>
                      <set-location ref="setLocation" @ok="setLocationOk" ></set-location>
                </a-col>
              </a-row>
            </a-form-model-item>
          </a-col> 
          <a-col :span="12">
            <a-form-model-item label="设备信号强度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rssi">
              <a-input-number v-model="model.rssi" placeholder="设备信号强度：0-100" style="width: 100%" disabled/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="设备SIM卡标识号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imsi">
              <a-input v-model="model.imsi" placeholder="请输入设备SIM卡标识号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所属项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectId">
              <j-dict-select-tag type="list" v-model="model.projectId" dictCode="tb_project,project_name,id" placeholder="请选择所属项目id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所属分组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="groupId">
              <j-dict-select-tag type="list" v-model="model.groupId" dictCode="tb_group,group_name,id" placeholder="请选择所属分组id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所属基站" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pci">
              <a-input v-model="model.pci" placeholder="请输入所属基站"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="设备状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="device_status" placeholder="请选择设备状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="在线状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="olStatus"> 
               <j-switch v-model="model.olStatus" :options="['1','0']"  ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="开关状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="onStatus">
              <j-switch v-model="model.onStatus"  :options="['1','0']" ></j-switch>
            </a-form-model-item>
          </a-col>
          
          <a-col :span="12">
            <a-form-model-item label="灯具类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lampType">
             <j-dict-select-tag type="list" v-model="model.lampType" dictCode="lamp_type" placeholder="请选择灯具类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="硬件版本号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hver">
              <a-input v-model="model.hver" placeholder="请输入硬件版本号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="软件版本号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sver">
              <a-input v-model="model.sver" placeholder="请输入软件版本号"  ></a-input>
            </a-form-model-item>
          </a-col>
            <a-col :span="12">
            <a-form-model-item label="所属云盒mac地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mac">
              <a-input v-model="model.mac" placeholder="请输入所属云盒mac地址"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row> 
      </a-form-model>
    </j-form-container> 
  </a-spin>  
</template> 
<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
 import SetLocation from './SetLocation'
  export default {
    name: 'LampForm',
    components: {
      SetLocation
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          gwlongitude:'',
          gwlatitude:'',
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           name: [
              { required: true, message: '请输入名称!'},
           ],
           code: [
              { required: true, message: '请输入灯控器地址!'},
           ],
           tmstamp: [
              { required: true, message: '请输入时间戳!'},
           ],
        },
        url: {
          add: "/corp/lamp/add",
          edit: "/corp/lamp/edit",
          queryById: "/corp/lamp/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    mounted(){
      
    },
    methods: {
      openSetLocation(){  
        this.$refs.setLocation.openDialog(this.model.gwlongitude,this.model.gwlatitude);
      },
      setLocationOk(longitude,latitude){ 
        this.model.gwlongitude=longitude;
        this.model.gwlatitude=latitude;
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>